package com.android.cookbook.activity;

import java.io.IOException;

import com.android.cookbook.R;
import com.android.cookbook.bean.Recipe;
import com.android.cookbook.db.BaseDatosHelper;
import com.android.cookbook.util.Constant;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailDriver extends Activity{

	BaseDatosHelper myDBHelper;
	String itemId;
	int intIsFavorite;
	ImageView favorite;
	String recipeId;
	
	/** Called when the activity is first created. */    
	@Override    
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);         
		setContentView(R.layout.item_detail); 
		createDB();
		Recipe recipeDetail = new Recipe();
		Bundle extras = getIntent().getExtras();
		
		if (extras != null) {             
			itemId = extras.getString(Constant.TYPE_ID);
			recipeDetail = getRecipe(Integer.parseInt(itemId));

			TextView controlTexto = (TextView) findViewById(R.id.titulo_detalle);             
			controlTexto.setText(recipeDetail.getName());       
			
			TextView controlDetalle = (TextView) findViewById(R.id.descripcion);
			controlDetalle.setText(recipeDetail.getDescription());
			
			TextView controlIngredients = (TextView) findViewById(R.id.ingredientes);
			controlIngredients.setText(recipeDetail.getIngredientes());
			
			TextView controlDirect = (TextView) findViewById(R.id.preparacion);
			controlDirect.setText(recipeDetail.getDirection());
		}
		
		favorite = (ImageView) findViewById(R.id.add_favorite);
		recipeId = String.valueOf(recipeDetail.get_id());
		intIsFavorite = isFavorite(recipeId);
		if (intIsFavorite==1){
			favorite.setImageResource(R.drawable.favorite);
		}else{
			favorite.setImageResource(R.drawable.add_favorite);
		}
		
		favorite.setOnClickListener(new View.OnClickListener() {             
        	public void onClick(View view) {  
        		intIsFavorite = isFavorite(recipeId);
        		if (intIsFavorite==1){
        			deleteFavorite(recipeId);
        			favorite.setImageResource(R.drawable.add_favorite);
        		}else{
        			insertFavorite(recipeId);
        			favorite.setImageResource(R.drawable.favorite);
        		}
        	}          
        });
		
		
	}
	
	public void createDB() {         
		myDBHelper = new BaseDatosHelper(this);         
		try {             
			myDBHelper.createDataBase();         
		} catch (IOException ioe) {             
			throw new Error("Unable to create database");         
		}     
	} 
	
	public Recipe getRecipe(int type_id) {         
		myDBHelper.openDataBase();         
		Recipe recipe = myDBHelper.getRecipe(type_id);         
		myDBHelper.close();         
		return recipe;     
	}
	

    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater=getMenuInflater(); 
        inflater.inflate(R.menu.menu_detail,menu); 
        return true; 
    }

    public boolean onOptionsItemSelected (MenuItem item){
    	Intent myIntent;
        switch (item.getItemId()){ 
        	case R.id.menu_home: 
        		myIntent = new Intent(this,CookBookActivity.class);
        		startActivityForResult(myIntent, 0);
        		return true;        
            case R.id.menu_search: 
            	myIntent = new Intent(this,RecipeSearch.class);
            	startActivityForResult(myIntent, 0);
            	return true;
            case R.id.menu_create: 
            	myIntent = new Intent(this,RecipeCreate.class);
            	startActivityForResult(myIntent, 0);
                return true;            
        }
        return super.onOptionsItemSelected(item);
    }

	public void insertFavorite(String item_id) {         
		myDBHelper.openDataBase();         
		myDBHelper.insertFavorite(item_id);       
		myDBHelper.close();         
		return;     
	}
	
	public int isFavorite(String item_id) {         
		int result;
		myDBHelper.openDataBase();         
		result = myDBHelper.isFavorite(item_id);       
		myDBHelper.close();         
		return result;     
	}

	public void deleteFavorite(String item_id) {         
		myDBHelper.openDataBase();         
		myDBHelper.deleteFavorite(item_id);       
		myDBHelper.close();         
		return;     
	}

}
